<template>
  <div>
    <div class="pageTitleWhole">
      <a-breadcrumb>
        <a-breadcrumb-item>车辆类型</a-breadcrumb-item>
      </a-breadcrumb>
    </div>
    <div id="takeoutMain">
      <a-form-model layout="inline" :model="form">
        <a-form-model-item label="类型名称">
          <a-input v-model="form.name" />
        </a-form-model-item>
        <a-form-model-item>
          <a-button type="primary" @click="check">查询</a-button>
        </a-form-model-item>
        <a-form-model-item>
          <a-button type="primary" @click="plus">+ 创建车辆类型</a-button>
        </a-form-model-item>
      </a-form-model>
      <a-table
        :columns="columns"
        :loading="loading"
        :data-source="data"
        :style="'margin-top: 25px;'"
        :pagination="pagination"
        :row-key="(record,index) => index"
      >
        <span slot="status" slot-scope="record" :style="'display: flex; align-items: center;'">
          <template v-if="record.status">
            <span class="green"></span>
            <span>正常</span>
          </template>
          <template v-else>
            <span class="red"></span>
            <span>冻结</span>
          </template>
        </span>
        <template slot="do" slot-scope="record">
          <a @click="edit(record)" v-if="record.status" style="margin-right:10px">编辑</a>
          <a @click="changeStatus(record)">
            <template v-if="record.status">冻结</template>
            <template v-else>恢复</template>
          </a>
        </template>
      </a-table>
    </div>
    <a-modal
      v-model="visible"
      :title="isCreate ? '创建车辆类型' : '编辑车辆类型'"
      :body-style="{padding:'50px 40px 30px 20px'}"
      centered
      @ok="modalOK"
      @cancel="modalCancel"
    >
      <a-form-model
        ref="modalForm"
        :rules="rules"
        :model="modalForm"
        :label-col="{span:6}"
        :wrapper-col="{span:18}"
      >
        <!-- 系统 -->
        <a-form-model-item label="类型名称" prop="name">
          <a-input v-model="modalForm.name" :max-length="20" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>
<script>
import {
  queryDataDictionaryList,
  updateDataDictionaryStatus,
  addDataDictionary
} from '@/api/landCenter'
const formMessage = `必填项不能为空`
const columns = [
  {
    title: '序号',
    width: 120,
    customRender: (text, record, index) => {
      return index + 1
    }
  },
  {
    title: '类型名称',
    dataIndex: 'name'
  },
  {
    title: '状态',
    key: 'stauts',
    scopedSlots: {
      customRender: 'status'
    }
  },
  {
    title: '操作',
    width: 160,
    key: 'do',
    scopedSlots: {
      customRender: 'do'
    }
  }
]
export default {
  data () {
    return {
      form: {
        type: 'TRUCK_TYPE',
        name: ''
      },
      pageNum: 1,
      pageSize: 10,
      loading: true,
      data: [],
      columns,
      pagination: {},
      isCreate: false,
      visible: false,
      modalForm: {
        name: '',
        type: 'TRUCK_TYPE'
      },
      rules: {
        name: [{ required: true, message: formMessage, trigger: 'blur' }]
      },
      dictionaryId: ''
    }
  },
  methods: {
    dictionaryListRequest (paramData) {
      this.loading = true
      queryDataDictionaryList(paramData)
        .then(res => {
          // // console.log(res)
          this.data = res.data.list
          let currentPage = this.pageNum
          this.pagination = {
            current: currentPage,
            pageSize: res.data.pageSize,
            total: res.data.total,
            showQuickJumper: false,
            showSizeChanger: true,
            showTotal: total =>
              `共${total}条记录 第${currentPage}/${res.data.pages}页`,
            onChange: pageNum => {
              this.pageNum = pageNum
              let paramData = {
                pageSize: this.pageSize,
                pageNum: this.pageNum,
                ...this.form
              }
              this.dictionaryListRequest(paramData)
            },
            onShowSizeChange: (current, size) => {
              this.pageNum = current
              this.pageSize = size
              let paramData = {
                pageSize: this.pageSize,
                pageNum: this.pageNum,
                ...this.form
              }
              this.dictionaryListRequest(paramData)
            }
          }
          this.loading = false
        })
        .catch(rs => {})
    },
    changeStatusRequest (param) {
      updateDataDictionaryStatus(param)
        .then(res => {
          if (res.code === 0) {
            this.$message.success('修改成功！')
            this.pageNum = 1
            this.pageSize = 10
            let paramData = {
              pageSize: this.pageSize,
              pageNum: this.pageNum,
              ...this.form
            }
            this.dictionaryListRequest(paramData)
          }
        })
        .catch(rs => {})
    },
    addOrEditRequest (e) {
      addDataDictionary(e)
        .then(res => {
          if (res.code === 0) {
            this.$message.success('操作成功！')
            this.pageNum = 1
            this.pageSize = 10
            let paramData = {
              pageSize: this.pageSize,
              pageNum: this.pageNum,
              ...this.form
            }
            setTimeout(() => {
              this.visible = false
              this.dictionaryListRequest(paramData)
            }, 300)
          }
        })
        .catch(rs => {})
    },
    check () {
      this.pageNum = 1
      let paramData = {
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        ...this.form
      }
      this.dictionaryListRequest(paramData)
    },
    plus () {
      if (!this.$root._bc.create) {
        this.$message.error('您无此权限！')
        return
      }
      this.isCreate = true
      this.visible = true
      this.dictionaryId = ''
      this.modalForm.name = ''
    },
    edit (e) {
      if (!this.$root._bc.edit) {
        this.$message.error('您无此权限！')
        return
      }
      this.isCreate = false
      this.visible = true
      this.modalForm.name = e.name
      this.dictionaryId = e.dictionaryId
    },
    changeStatus (e) {
      if (!this.$root._bc.frozen) {
        this.$message.error('您无此权限！')
        return
      }
      let _t = this
      _t.$confirm({
        title: `${e.status ? '确定冻结吗？' : '确定恢复正常吗？'}`,
        onOk () {
          _t.changeStatusRequest({
            dictionaryId: e.dictionaryId,
            status: e.status ? 0 : 1
          })
        }
      })
    },
    modalOK () {
      let _t = this
      _t.$refs.modalForm.validate(valid => {
        let paramId = _t.isCreate ? null : _t.dictionaryId
        if (valid) {
          _t.addOrEditRequest({
            ..._t.modalForm,
            dictionaryId: paramId
          })
        }
      })
    },
    modalCancel () {}
  },
  mounted () {
    this.dictionaryListRequest({
      ...this.form,
      pageSize: this.pageSize,
      pageNum: this.pageNum
    })
  }
}
</script>
<style lang="less" scoped>
#takeoutMain {
  padding: 24px;
  margin: 24px;
  background: #fff;
  min-height: 700px;
  .green {
    display: inline-block;
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: #00a854;
    margin-right: 5px;
  }
  .red {
    display: inline-block;
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: #f04134;
    margin-right: 5px;
  }
}
</style>
